/**
 * 远光ECP平台3.0版本 http://www.ygsoft.com/ Grace [Gris Application Cloud-Computing
 * Extension]
 * Copyright(c) 1998 - 2020 YGSoft.Inc, All Rights Reserved
 * 
 * ecp.component.simpleUpload.js
 * 
 * 单个项目上传控件
 * 
 * 占位符，例如：
 * <div class="input-prepend input-group" style="width:30%">
 *	<input id="attachment" data-option="{"resId":"daf12ddf98s0sf43lfsf090"}" class="form-control attachment-input">
 *	<span class="add-on input-group-addon">
 *		<i class="glyphicon attachment-i">...</i>
 *	</span>
 * </div>
 * 
 * 变更版本：
 * Created by zhengxianwei@ygsoft.com 2016-07-13
 */
define(["jquery","ecp.utils", "ecp.component.upload", "ecp.utils.i18n"],function($, utils, upload, i18n){
	/**
	 * 默认参数.
	 */
	var defaultOption = {
		webuploader: {
			pick:{
				multiple:false
			}
		}
	};
	/**
	 * 声明EcpUploader对象.
	 */
	var SimpleUploader = function(control,option){
		if(!control || control.length === 0){return;}
		this.init(control,option);
		this.render();
	}
	
	/**
	 * 通过原型定义相关方法，比如渲染、赋值等等.
	 */
	SimpleUploader.prototype = {
		/**
		 * 初始化.
		 * @param {Object} control 占位符
		 * @param {Object} option 控件渲染配置属性
		 */
		init: function(control,option){
			if(!control || control.length === 0){
				if(window.console){
					window.console.error(i18n.upload.notcontrol);
				}
				return;
			}
			this.control = control;
			var dOpt = control.attr("data-option");
			if(dOpt && dOpt.length !== 0){
				dOpt = $.parseJSON(dOpt);
			}else{
				dOpt = {};
			}
			this.option = $.extend(true, {}, defaultOption, option, dOpt);
		},
		
		/**
		 * 渲染.
		 */
		render: function(){
			this.upload = this.control.ecpUploader(this.option);
		},
		
		/**
		 * 设置或获取附件值.
		 */
		value: function(value){
			 var me = this,
			 	 ct = me.control;
			 if(arguments.length > 0){
				 me.initValue(value);
			 }else{
				 return ct.val();
			 }
		},
		
		/**
		 * 设置显示值.
		 */
		initValue: function(value){
			if(arguments.length === 0){return;}
		 	this.control.val(value);
		 	this.option.resId = value;
		},
		
		/**
		 * 设置只读.
		 */
		readOnly:function(){
			this.control.attr("readOnly","readOnly");
		},
		
		/**
		 * 移除只读.
		 */
		removeReadOnly: function(){
			this.control.removeAttr("readOnly");
		},
		
		/**
		 * 设置不可用.
		 */
		disable: function(){
			this.control.attr("disabled",true);
			if(this.upload){
				this.upload.disable = true;
				if(this.upload.control){
					this.upload.control.next().css("cursor","default");
				}
			}
		},
		
		/**
		 * 设置可用.
		 */
		enable: function(){
			this.control.removeAttr("disabled");
			if(this.upload){
				this.upload.disable = false;
				if(this.upload.control){
					this.upload.control.next().css("cursor","pointor");
				}
			}
		},
		/**
		 * 设置ywKey.
		 */
		setYwKey: function(key){
			this.upload.setYwKey(key);
		},
		/**
		 * 获取ywKey.
		 */
		getYwKey: function(){
			 return this.upload.getYwKey();
		}
	};
	(function($) {
		var old = $.fn.simpleUploader;
		$.fn.simpleUploader = function(option){
			var me = this;
			option = option || {};
			var type = typeof(option);
			//如果第一个参数传入的是对象或空则初始化空件，如果传入的是字符串，则认为是调用里面的内部方法。
			if (type === "object") {
				var eus = [];
				this.each(function() {
					var instanceOption = $.extend({}, option, true);
					var instance = new SimpleUploader($(this), instanceOption);
					this.simpleUploader = instance;
					eus.push(instance);
				});
				if(eus.length === 1){
					eus = eus[0];
				}
				return eus;
			} else if (type === "string") {
				var args = Array.prototype.slice.call(arguments, 1);
				if (SimpleUploader.prototype[option]) {
					return SimpleUploader.prototype[option].call(args);
				}
			} else if (option === true) {
				return me[0] && me[0].simpleUploader;
			}
		};
		$.fn.simpleUploader.Constructor = SimpleUploader;
		$.fn.simpleUploader.noConflict = function() {
			$.fn.simpleUploader = old;
			return this;
		};
	}(jQuery));
	return SimpleUploader.prototype;
		
});